package tnviet.proj.jna.hexarea;

import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JTextPane;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import jpcap.packet.Packet;

/**
 *
 * @author Administrator
 */
public class JNHexPane extends JTextPane{
    //JTextArea textArea;
    String  FIRST_GAP = " &nbsp&nbsp ";
    String SECOND_GAP = " &nbsp&nbsp ";
    Font font = new Font("Courier New", Font.PLAIN, 13);

    public JNHexPane() {
        super();
        this.setEditable(false);
        this.setMinimumSize(new Dimension(0, 0));
    }

    public void showPacket(Packet packet){
        byte[] bytes = new byte[packet.header.length + packet.data.length];
        System.arraycopy(packet.header,0,bytes,0,packet.header.length);
        System.arraycopy(packet.data,0,bytes,packet.header.length,packet.data.length);
        StringBuffer buf = new StringBuffer();
        buf.append("<html><body>");
        int lineCount = bytes.length/16;
        int remainder = bytes.length % 16;
        for(int i =0; i<lineCount;i++){
            String d = Integer.toHexString(i*16);
            int n = d.length();
            for(int k =0; k<4-n;k++){
                d = "0" + d;
            }
            buf.append(d);
            buf.append(FIRST_GAP);
            for(int j=0; j< 16; j++){
                String a =Integer.toHexString((int)(bytes[i*16+j]&0xff));
                if(bytes[i*16+j]<32 || bytes[i*16+j]>126) bytes[i*16+j]=46;
                buf.append((a.length()==1?"0"+a:a)+" ");
            }
            buf.append(SECOND_GAP);
            buf.append("<plaintext>");
            buf.append(new String(bytes,i*16,16));
            buf.append("</plaintext>");
            buf.append("<br>");
        }
        String d2 = Integer.toHexString(lineCount*16);
        int n2 = d2.length();
        for(int k =0; k<4-n2;k++){
            d2 = "0" + d2;
        }
        buf.append(d2);
        buf.append(FIRST_GAP);
        for(int i=16*lineCount;i<bytes.length;i++){
            String a =Integer.toHexString((int)(bytes[i]&0xff));
            if(bytes[i]<=32 || bytes[i]>126) bytes[i]=46;
            buf.append((a.length()==1?"0"+a:a)+" ");
        }
        for(int i=0; i<16-remainder-1; i++){
            buf.append("&nbsp&nbsp&nbsp");
        }
        buf.append("&nbsp&nbsp ");
        buf.append(SECOND_GAP);
        buf.append("<plaintext>");
        buf.append(new String(bytes,lineCount*16,remainder));
        buf.append("</plaintext>");
        buf.append("<br>");
        buf.append("</body></html>");
        this.setContentType("text/html");
        this.setText(buf.toString());
        this.setCaretPosition(0);

        MutableAttributeSet attrs = this.getInputAttributes();
        StyleConstants.setFontFamily(attrs, font.getFamily());
        StyleConstants.setFontSize(attrs, font.getSize());
        StyleConstants.setItalic(attrs, (font.getStyle() & Font.ITALIC) != 0);
        StyleConstants.setBold(attrs, (font.getStyle() & Font.BOLD) != 0);
        StyledDocument doc = this.getStyledDocument();
        doc.setCharacterAttributes(0, doc.getLength() + 1, attrs, false);
    }

    @Override
    public boolean getScrollableTracksViewportWidth()
    {
       return false;
    }


    public static void main(String[] args){

        for(int i =0; i<100;i++){
            String d = Integer.toHexString(i*16);
            int n = d.length();
            for(int k =0; k<4-n;k++){
                d = "0" + d;
            }
            System.out.println(d);
        }
    }



}
